package com.koron.ticket.operation.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.common.core.util.CodeTools;
import com.koron.ticket.enums.ApprovEnum;
import com.koron.ticket.operation.bean.WTicketSecurity;
import com.koron.ticket.operation.bean.WTicketSecuritySteps;
import com.koron.ticket.operation.mapper.WTicketSecurityMapper;
import com.koron.ticket.operation.mapper.WTicketSecurityStepsMapper;
import com.koron.ticket.operation.service.WTicketSecurityService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * 安全防误操作票(WTicketSecurity)表服务实现类
 *
 * @author makejava
 * @since 2023-06-25 09:24:08
 */
@Service
public class WTicketSecurityServiceImpl implements WTicketSecurityService {


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    @TaskAnnotation("queryById")
    public WTicketSecurity queryById(SessionFactory factory, String id) {
        WTicketSecurity wTicketSecurity = factory.getMapper(WTicketSecurityMapper.class).queryById(id);
        if (!ObjectUtils.isEmpty(wTicketSecurity)){
            List<WTicketSecuritySteps> wTicketSecuritySteps = factory.getMapper(WTicketSecurityStepsMapper.class).queryByObjId(wTicketSecurity.getObjId());
            wTicketSecurity.setStepsList(wTicketSecuritySteps);
        }
        return wTicketSecurity;
    }

    /**
     * 分页查询
     *
     * @param wTicketSecurity 筛选条件
     * @return 查询结果
     */
    @Override
    @TaskAnnotation("queryByPage")
    public PageInfo<WTicketSecurity> queryByPage(SessionFactory factory, WTicketSecurity wTicketSecurity) {
        PageHelper.startPage(wTicketSecurity.getPage(), wTicketSecurity.getPageSize());
        if (CollectionUtils.isEmpty(wTicketSecurity.getStatusList())){
            wTicketSecurity.setStatusList(null);
        }
        List<WTicketSecurity> wTicketSecurities = factory.getMapper(WTicketSecurityMapper.class).queryAllByLimit(wTicketSecurity);
        return new PageInfo<>(wTicketSecurities);
    }

    /**
     * 新增数据
     *
     * @param wTicketSecurity 实例对象
     * @return 实例对象
     */
    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, WTicketSecurity wTicketSecurity) {
        wTicketSecurity.setId(CodeTools.getCode32());
        wTicketSecurity.setStatus(ApprovEnum.ADD.getValue());
        wTicketSecurity.setCreateTime(new Date());
        int update = factory.getMapper(WTicketSecurityMapper.class).insert(wTicketSecurity);
        int u1 = 0;
        if (update >= 1) {
            wTicketSecurity.getStepsList().stream().forEach(wTicketSecuritySteps -> {
                wTicketSecuritySteps.setObjId(wTicketSecurity.getObjId());
                wTicketSecuritySteps.setId(CodeTools.getCode32());
                wTicketSecuritySteps.setCreateTime(new Date());
            });
            u1= factory.getMapper(WTicketSecurityStepsMapper.class).insertBatch(wTicketSecurity.getStepsList());
        }
        if (update>0&&u1>0){
            return 0;
        }
        return -1;
    }

    /**
     * 修改数据
     *
     * @param wTicketSecurity 实例对象
     * @return 实例对象
     */
    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, WTicketSecurity wTicketSecurity) {
        wTicketSecurity.setUpdateTime(new Date());
        int update = factory.getMapper(WTicketSecurityMapper.class).update(wTicketSecurity);
        int u1 = 0;
        if (update >= 1) {
            wTicketSecurity.getStepsList().stream().forEach(wTicketSecuritySteps -> {
                wTicketSecuritySteps.setObjId(wTicketSecurity.getObjId());
                wTicketSecuritySteps.setUpdateTime(new Date());
            });
            u1= factory.getMapper(WTicketSecurityStepsMapper.class).insertOrUpdateBatch(wTicketSecurity.getStepsList());
        }
        if (update>0&&u1>0){
            return 0;
        }
        return -1;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {
        return factory.getMapper(WTicketSecurityMapper.class).deleteById(id);
    }
}
